package com.bobi.ecofavor.model.domain;

import com.baomidou.mybatisplus.annotation.*;

import java.math.BigDecimal;

import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.annotation.TableId;

import java.time.LocalDateTime;
import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;

/**
 * <p>
 * 优惠券种类
 * </p>
 *
 * @author wenyuan
 * @since 2023-10-21
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("bobi_coupon_category")
@ApiModel(value = "CouponCategory对象", description = "优惠券种类")
public class CouponCategory extends Model<CouponCategory> {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "优惠券主键")
    @TableId(value = "id", type = IdType.AUTO)
    private Integer id;

    @ApiModelProperty(value = "优惠券名称")
    private String couponName;

    @ApiModelProperty(value = "作用范围（全平台、指定商户、指定分类））")
    private Integer useRange;

    @ApiModelProperty(value = "作用店铺分类（全平台是即为通用）")
    private Integer storeCategory;

    @ApiModelProperty(value = "店铺id")
    private Integer storeId;

    @ApiModelProperty(value = "上/下架")
    private Boolean onList;

    @ApiModelProperty(value = "优惠券状态（领完、有余）")
    private Integer couponStatus;

    @ApiModelProperty(value = "发放数量")
    private Integer distributedQuantity;

    @ApiModelProperty(value = "剩余数量")
    private Integer remainQuantity;

    @ApiModelProperty(value = "优惠条件")
    private Integer couponCondition;

    @ApiModelProperty(value = "要求金额")
    private BigDecimal requiredAmount;

    @ApiModelProperty(value = "减免金额")
    private BigDecimal cutAmount;

    @ApiModelProperty(value = "折扣大小")
    private BigDecimal discount;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(value = "开始时间，可使用时间")
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime beginTime;

    @ApiModelProperty(value = "领取后有效持续时间")
    private Integer validTime;

    // 默认2099年-9-9
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(value = "结束时间")
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime endTime;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(value = "创建时间")
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime createTime;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty(value = "更改时间")
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    private LocalDateTime updateTime;

    @TableLogic
    @ApiModelProperty(value = "逻辑删除")
    private Boolean isDeleted;


    @Override
    public Serializable pkVal() {
        return this.id;
    }


    /**
     * 优惠券的结束时间 = 领取时间 + 持续时间
     * 如果 领取时间 + 持续时间 > 优惠券种类结束时间，则结束时间即为优惠券种类结束时间
     * @return LocalDateTime
     */
    public LocalDateTime getFinalEndTime() {
        LocalDateTime localDateTime = beginTime.plusMinutes(validTime);
        if (localDateTime.isAfter(endTime))
            return endTime;
        else
            return localDateTime;
    }
}
